
package com.fenghongzhang.module_main.glide.glide
import com.bumptech.glide.load.model.GlideUrl

/**
 *@author Wcj
 *@description
 *@date 2022/7/13 9:01
 *
 * https://moto-test.oss-cn-hangzhou.aliyuncs.com/1657623347295/-557463384.jpg?Expires=1689209997&OSSAccessKeyId=LTAI4GEkT64tpLbw38Tp3kK6&Signature=AX1fejAFFHsTzgepP6%2FqMRjZoJk%3D
 */
class AliOssUrl(private val url: String?) : GlideUrl(url) {

    override fun getCacheKey(): String {
        var transUrl = url ?: super.getCacheKey() //url?.replace(getTokenParam(), "") ?: super.getCacheKey()
        if (transUrl.endsWith(".mp4"))
            transUrl += "?x-oss-process=video/snapshot,t_75,m_fast"
        return transUrl
    }

    private fun getTokenParam(): String {
        if (url.isNullOrEmpty()) {
            return ""
        }
        var tokenParam = ""
        val tokenKeyIndex = if (url.indexOf("?Expires=") > 0) url.indexOf("?Expires=") else url.indexOf("&Expires=")
        if (tokenKeyIndex != -1) {
            tokenParam = url.substring(tokenKeyIndex, url.length)
        }
        return tokenParam
    }


}